package org.infinity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author cfly
 */
public final class FileMap extends HashMap<String, Long> {
	private static final Log log = LogFactory.getLog(FileMap.class);

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1010780903372883757L;

	private static FileMap fileMap;

	private static final String POSTFIX = "idx";
	/** dot */
	public static final String DOT = ".";

	private FileMap() {
	}

	synchronized public static FileMap getInstance() {
		if (fileMap == null) {
			fileMap = loadFileMap();
		}
		return fileMap;
	}

	public static FileMap loadFileMap() {
		log.debug("loadFileMap   start");
		try {
			fileMap = (FileMap) Util.loadObject(Util.encode(Config
					.getInstance().getDestPath().getAbsolutePath())
					+ DOT + POSTFIX);
		} catch (FileNotFoundException e) {
			fileMap = new FileMap();
			File file = Config.getInstance().getDestPath();
			initFile2Map(file);
			saveFileMap(fileMap);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e);
		}
		// saveFileMap(fileMap);
		log.debug("loadFileMap   end");
		return fileMap;
	}

	public static boolean initial() {
		log.debug("initial   start");
		File file = Config.getInstance().getDestPath();
		if (!Util.checkPath(file)) {
			return false;
		}
		getInstance();
		log.debug("initial   end");
		return true;
	}

	private static void initFile2Map(File file) {
//		log.debug("initFile2Map   start");
		log.debug("file:" + file.getAbsolutePath());
		File[] files = file.listFiles();
		if (files == null) {
			return;
		}
		for (File subFile : files) {
			if (subFile.isDirectory()) {
				initFile2Map(subFile);
			} else {
				FileMap.getInstance().put(subFile.getAbsolutePath(),
						subFile.lastModified());
				// File file2 = FileMap.getInstance().get(
				// subFile.getAbsoluteFile());
				// if (judgeFile(subFile, file2)) {
				// Util.showMessage("info.001", subFile.getAbsoluteFile(),
				// new Date(subFile.lastModified()), new Date(file2
				// .lastModified()));
				// FileMap.getInstance().put(subFile.getAbsolutePath(),
				// subFile);
				// }
			}
		}
//		log.debug("initFile2Map   end");
	}

	public void saveFileMap() {
		log.debug("saveFileMap   start");
		Util.saveObject(Util.encode(Config.getInstance().getDestPath()
				.getAbsolutePath())
				+ DOT + POSTFIX, this);
		log.debug("saveFileMap   end");
	}

	public static void saveFileMap(Object obj) {
		log.debug("saveFileMap   start");
		Util.saveObject(Util.encode(Config.getInstance().getDestPath()
				.getAbsolutePath())
				+ DOT + POSTFIX, obj);
		log.debug("saveFileMap   end");
	}

	@Override
	public Long put(String key, Long value) {
		return super.put(key, value);
	}

	@Override
	public void putAll(Map<? extends String, ? extends Long> m) {
		super.putAll(m);
	}
}
